﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class AdminFancy : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    BettingType bettingType;
    List<BettingType> bettingTypes;
    MatchType matchType;
    List<MatchType> matchTypes;
    FancyNineEleven betting;
    static IDataRepository _dataObject1 = new DataRepository();
    static FancyNineEleven bet;
    static MatchSchedule ms1;
    static CricketGeneral crickGen;
    public string id = "";
    

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                if (cookie["BetId"] == "1")
                {
                    drpBettingType.SelectedValue="2";
                    drpBettingType.Enabled = false;
                }
                else if (cookie["BetId"] == "2")
                {
                    drpBettingType.SelectedValue = "1";
                    drpBettingType.Enabled = false;
                }
                else if (cookie["BetId"] == "3")
                {
                    drpBettingType.SelectedValue = "5";
                    drpBettingType.Enabled = false;
                }
                hdnAdminId.Value = cookie["AdminId"]; 
                bindBettingType();
                bindMatchType();
                if (Request.QueryString["Id"] != null)
                    getDropdownsSelected(Convert.ToInt32(Request.QueryString["Id"].ToString()));
            }
        }
    }

    private void bindMatch(int matchType)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheResult> lstResult = new List<GetTodayMatcheResult>();
        lstResult = matchSchedule.GetTodayMatches(matchType);
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
        else
        {
            drpMatch.DataSource = null;           
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }

    private void bindMatchType()
    {
        matchType = new MatchType(_dataObject);
        matchTypes = new List<MatchType>();
        matchTypes = matchType.GetAllMatchType();
        if (matchTypes.Count > 0)
        {
            drpMatchType.DataSource = matchTypes;
            drpMatchType.DataTextField = "Type";
            drpMatchType.DataValueField = "Id";
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
    }

    private void bindBettingType()
    {
        bettingType = new BettingType(_dataObject);
        bettingTypes = new List<BettingType>();
        bettingTypes = bettingType.GetAllBettingType();
        if (bettingTypes.Count > 0)
        {
            drpBettingType.DataSource = bettingTypes;
            drpBettingType.DataTextField = "Type";
            drpBettingType.DataValueField = "Id";
            drpBettingType.DataBind();
            drpBettingType.Items.Insert(0, new ListItem("Select Betting Type", "0"));
        }
    }
    protected void drpMatchType_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindMatch(Convert.ToInt32(drpMatchType.SelectedValue));
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMatch.SelectedItem.Text.ToLower() != "select match" && drpMatch.SelectedItem.Text.ToLower() != "select match type")
        {
            matchSchedule = new MatchSchedule(_dataObject);
            List<GetMatchDetailsResult> lstResult = new List<GetMatchDetailsResult>();
            lstResult = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
            lblTeamA.Text = lstResult[0].Abb1;
            lblTeamB.Text = lstResult[0].Abb2;
            hdnTeamAId.Value = Convert.ToString(lstResult[0].TeamAId);
            hdnTeamBId.Value = Convert.ToString(lstResult[0].TeamBId);
        }
    }



    [WebMethod]
    public static int SaveBettingRecords(int bettingTypeId, int matchTypeId, int matchScheduleId, int teamId, string over, string lagadiRate)
    {
        var response = HttpContext.Current.Request;

        HttpCookie cookie = response.Cookies["Admin"];
        var adminId = "";
        if (cookie["AdminId"] != null)
            adminId = cookie["AdminId"];

        bet = new FancyNineEleven(_dataObject1);
        crickGen = new CricketGeneral();
        bet.BettingTypeId = bettingTypeId;
        bet.AdminId = Convert.ToInt32(adminId);
        bet.MatchTypeId = matchTypeId;
        bet.MatchScheduleId = matchScheduleId;
        bet.Over = over;
        bet.TeamId = teamId;
        bet.BettingRate = lagadiRate;
        
        bet.IsDeleted = false;
        
        bet.CreatedDate = System.DateTime.Now;
        bet.IPAdded = crickGen.IPAddress(System.Web.HttpContext.Current);
        bet.CreateFancyNineEleven();

        return 1;
    }

    [WebMethod]
    public static int UpdateBettingRecords(int bettingTypeId)
    {

        var response = HttpContext.Current.Request;
        HttpCookie cookie = response.Cookies["Admin"];
        var adminId = "";
        if (cookie["AdminId"] != null)
            adminId = cookie["AdminId"];

        bet = new FancyNineEleven(_dataObject1);
        bet.IsDeleted = true;
        bet.BettingTypeId = bettingTypeId;
        bet.AdminId = Convert.ToInt32(adminId);
        bet.UpdateFancyNineEleven();
        return 1;
    } 

    
    
    
    private void getDropdownsSelected(int matchScheduleId)
    {
        ////DataTable dt = GetData("select * from MatchSchedule where Id = " + matchScheduleId.ToString());
        //drpMatchType.SelectedValue = dt.Rows[0]["MatchType"].ToString();
        //bindMatch(Convert.ToInt32(drpMatchType.SelectedValue));
        //drpMatch.SelectedValue = matchScheduleId.ToString();
        //drpMatch_SelectedIndexChanged(null, null);
    }
    

    

    
}